/** Display Database ResultSet in JTable.
    The program does the following:
	- Make a connection
	- Create a statement, and result set,
	- manuallly fill the cells in JTable object.
   For a better example of display database tablem see JTableJdbc.java and JDBC2TableModel.java
*/

import java.sql.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;

public class AccessChevalDB4 extends CloseableJFrame implements ActionListener {
	// Database related objects
	Connection cnn;
	Statement  stmt;
	ResultSet  res;
	ResultSetMetaData meta;
	Vector  colNames ;
	Vector	rows ;	// Vector of Vectors (All columns of a row will be a vector)
	JTable	grid;
	JScrollPane srpnGrid = null;

	// GUI related objects
	Container c;
	JLabel  lblSQL = new JLabel("SQL ", SwingConstants.RIGHT);
	JTextField tfSQL = new JTextField("SELECT * FROM EMP", 40);
	JPanel  pnSouth = new JPanel();

	String url = "jdbc:oracle:thin:@cheval.cs.csubak.edu:1521:TEST",
		 user="scott", passwd="tiger";

	public AccessChevalDB4() {
		try {
 			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			cnn = DriverManager.getConnection(url, user, passwd);
			stmt = cnn.createStatement();
		   } catch (SQLException e ) { e.printStackTrace(); System.exit(-1); }
		c = getContentPane(); c.setLayout(new BorderLayout());
		tfSQL.addActionListener(this);
		pnSouth.add(lblSQL); pnSouth.add(tfSQL);
		c.add(pnSouth, BorderLayout.SOUTH);
		setSize(600, 400);
		setTitle("Enter a SQL Statement");
		show();
	}

	public static void main(String argv[]) {
		AccessChevalDB4 ex4 = new AccessChevalDB4(); // get connected, and create stmt.
	}

	public void actionPerformed (ActionEvent e) {
		int colCnt = 0;
		executeSQL(tfSQL.getText());
		try { colCnt = meta.getColumnCount(); } catch (SQLException sqlEx) { sqlEx.printStackTrace(); }

		rows = new Vector(); colNames = new Vector();
		fillColumnNameVector(colNames, colCnt);
		fillRows(rows, colCnt);

		setTitle("Result of " + tfSQL.getText().trim());
		
		if (srpnGrid != null) c.remove(srpnGrid);
		grid = new JTable(rows, colNames);
		srpnGrid = new JScrollPane(grid);
		c.add(srpnGrid, BorderLayout.CENTER);
		validate();
	}

	void executeSQL(String sql) {
	   try {
		if ( sql.charAt(sql.length() -1) == ';' ) sql = sql.substring(0, sql.length() -1);
		res = stmt.executeQuery(sql);
		meta = res.getMetaData();
	   } catch (SQLException e) { System.err.println("\nError occured in executing SQL: \n\t" + sql + "\n"); }
	}

	void fillRows(Vector v, int colCnt) {
	   try {
		while ( res.next() ) {
			Vector t = new Vector();
			for ( int i = 1; i <= colCnt; i ++ ) t.add(res.getString(i)) ;
			v.add(t);
		}	
	   } catch (SQLException e) { e.printStackTrace();  /*System.exit(-1);*/ }
	}

	void fillColumnNameVector(Vector v, int cnt) {
	   try {
		for ( int i = 1; i <= cnt; i ++ ) v.add(meta.getColumnName(i));
	   } catch (SQLException e ) { e.printStackTrace(); }
	
	}
	
	protected void finalize() {
	   try {
		stmt.close();
		res.close();
		cnn.close();
	   } catch (SQLException e) {}
	   try { super.finalize(); } catch (Throwable e) { e.printStackTrace(); }
	}
}
